package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.*;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.CartVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.CartMapper;
import com.trace.app.service.CartService;
import com.trace.app.service.ProductService;
import com.trace.app.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Cart)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-02 12:36:42
 */
@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private ProductService productService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        Page<Cart> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Cart> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Cart cart) {
        return save(cart) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Cart cart) {
        LambdaUpdateWrapper<Cart> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Cart::getId, cart.getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getByUserId(Integer id) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, id).orderByDesc(Cart::getCreateTime);
        List<Cart> cartList = list(queryWrapper);
        List<CartVo> collect = cartList.stream().map(cart -> {
            CartVo cartVo = BeanCopyUtils.copyBean(cart, CartVo.class);
            LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Product::getId, cart.getProId());
            Product product = productService.getOne(wrapper);
            cartVo.setName(product.getName())
                    .setImg(product.getImg())
                    .setPrice(product.getPrice());

            return cartVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }
}

